package com.zhaopin.thread;

import java.util.concurrent.TimeUnit;

/**
 * @author yin.peide
 * @date 2021-06-01 11:22
 */

public class ThreadJoin {
    public static void main(String[] args) throws InterruptedException {
        long startTimestamp = System.currentTimeMillis();
        Thread t1 = new Thread(new CaptureRunnable("M1", 10000L));
        Thread t2 = new Thread(new CaptureRunnable("M2", 30000L));
        Thread t3 = new Thread(new CaptureRunnable("M3", 15000L));

        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
        long endTimestamp = System.currentTimeMillis();
        long l =  endTimestamp-startTimestamp;
        System.out.println(TimeUnit.MILLISECONDS.toSeconds(l));
        System.out.printf("save data begin timestamp is:%s, end timestamp is%s", startTimestamp, endTimestamp);
    }

}

class CaptureRunnable implements Runnable {
    private String machineName;
    private long spendTime;

    public CaptureRunnable(String machineName, long spendTime) {
        this.machineName = machineName;
        this.spendTime = spendTime;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(spendTime);
            System.out.println(machineName + "completed data capture and success");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public String getResult() {
        return machineName + " finish.";
    }

}
